package com.atguigu.MyTest;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Scanner;

/**
 * @author haoranlu
 * @project IntelliJ IDEA
 */
public class TestBookDAOImp1 {
    BookDAO bd = new BookDAOImp1();
    @Test
    public void test01() {
        Scanner input = new Scanner(System.in);

        System.out.print("请输入书名：");
        String title = input.nextLine();

        System.out.print("请输入作者：");
        String author = input.nextLine();

        System.out.print("请输入价格：");
        double price = input.nextDouble();

        System.out.print("请输入销量：");
        int sales = input.nextInt();
        input.nextLine();

        System.out.print("请输入库存量：");
        int stock = input.nextInt();
        input.nextLine();

        System.out.print("请输入图片路径(例如：upload/books/从入门到精通.jpg：");
        String imgPath = input.nextLine();

        try {
            Books book = new Books(null,title, author, price, sales, stock, imgPath);
            bd.addBook(book);
            System.out.println("添加成功");
        } catch (Exception e) {
            System.out.println("添加失败，原因：" + e.getMessage());
        }

        input.close();
    }



    /*
     * 测试修改图书
     */
    @Test
    public void test02(){
        List<Books> allBook = bd.getAllBook();
        allBook.forEach(System.out::println);
        Scanner input = new Scanner(System.in);
        System.out.print("请输入要修改图书的编号:");
        String bookId = input.nextLine();

        Books book = bd.getBookById(bookId);
        System.out.println("直接回车表示该项不修改");
        System.out.print("请输入书名("+book.getTitle()+")：");
        String title = input.nextLine();
        if (title.length() != 0){
            book.setTitle(title);
        }

        System.out.print("请输入作者("+book.getAuthor()+")：");
        String author = input.nextLine();
        if (author.length() != 0){
            book.setAuthor(author);
        }

        System.out.print("请输入价格("+book.getPrice()+")：");
        String strPrice = input.nextLine();
        if(strPrice.length() != 0){
            book.setPrice(Double.parseDouble(strPrice));
        }

        System.out.print("请输入库存量("+book.getStock()+")：");
        String stockStr = input.nextLine();
        if(stockStr.length() != 0){
            book.setStock(Integer.parseInt(stockStr));
        }

        System.out.print("请输入图片路径("+book.getImgPath()+")：");
        String imgPath = input.nextLine();
        if(imgPath.length() != 0){
            book.setImgPath(imgPath);
        }

        try {
            bd.updateBook(book);
            System.out.println("修改成功");
        } catch (Exception e) {
            System.out.println("修改失败，原因：" + e.getMessage());
        }
        input.close();
    }
    @Test
    public void test03(){
        List<Books> list = bd.getAllBook();
        list.forEach(System.out::println);

        Scanner input = new Scanner(System.in);
        System.out.print("请输入删除图书的id:");
        String id = input.nextLine();

        try {
            bd.deleteBookById(id);
            System.out.println("删除成功");
        }catch (Exception e){
            System.out.println("删除失败"+e.getMessage());
        }
    }
    @Test
    public void test04(){
        BookDAO bd = new BookDAOImp1();
        Scanner input = new Scanner(System.in);

        Page<Books> page = new Page<>();
        page.setPageNo(1);

        boolean flag = true;
        while(flag){
            Page<Books> pageBooks = bd.getPagesBook(page);
            List<Books> list = pageBooks.getList();
            System.out.println("一共：" + page.getTotalPageNo() + "页，当前是第" + page.getPageNo() + "页");
            for (Books book : list) {
                System.out.println(book);
            }

            page = new Page<>();
            System.out.print("请选择页码：");
            int pageNo = input.nextInt();
            page.setPageNo(pageNo);
        }

        input.close();
    }

    @Test
    public void test05(){
        BookDAO bd = new BookDAOImp1();
        Scanner input = new Scanner(System.in);
        System.out.println("请输入价格范围：");
        System.out.print("最低价格：");
        double minPrice = input.nextDouble();

        System.out.print("最高价格：");
        double maxPrice = input.nextDouble();

        Page<Books> page = new Page<>();
        page.setPageNo(1);
        boolean flag = true;
        while(flag){
            Page<Books> pageBooks = bd.getPageBooksByPrice(page, minPrice, maxPrice);
            if(page.getTotalPageNo() == 0){
                System.out.println("该范围没有数据");
                break;
            }
            List<Books> list = pageBooks.getList();
            System.out.println("一共：" + page.getTotalPageNo() + "页，当前是第" + page.getPageNo() + "页");
            for (Books book : list) {
                System.out.println(book);
            }

            System.out.print("请选择页码：");
            int pageNo = input.nextInt();

            page.setPageNo(pageNo);
        }

        input.close();
    }
    @Test
    public static void main(String[] args) {

    }
}

